package com.df.tv.launcher.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.df.tv.launcher.R;

public class RatingView extends RelativeLayout {
    private RatingBar mRating;
    private TextView mScore1;
    private TextView mScore2;

    public RatingView(Context context) {
        super(context);
        init(context);
    }

    public RatingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mRatingView = inflater.inflate(R.layout.rating, this);
        mRating = (RatingBar) mRatingView.findViewById(R.id.rb_star);
        mScore1 = (TextView) mRatingView.findViewById(R.id.tv_score_1);
        mScore2 = (TextView) mRatingView.findViewById(R.id.tv_score_2);
        mRating.setFocusable(false);
        mRating.setClickable(false);
        mRating.setFocusableInTouchMode(false);
        setRating(0.0f);
    }

    public void setRating(float rating) {
        int stars = mRating.getNumStars();
        if (Math.abs(stars) > 0) {
            mRating.setRating((rating * stars * 5) / mRating.getMax());
        }
        String ratingStr = String.valueOf(rating);
        mScore1.setText(ratingStr.split("\\.")[0]);
        mScore2.setText("." + ratingStr.split("\\.")[1]);
    }

}
